#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "online_list.h"
#include "common.h"

void print_online_users(struct online_user* list) {
    printf("\n=== 在线用户 ===\n");
    struct online_user* current = list;
    int count = 1;

    while (current != NULL) {
        printf("%d. %s (%s)\n", count, current->username, current->account);
        current = current->next;
        count++;
    }
    printf("================\n");
    printf("> ");
    fflush(stdout);
}

void add_online_user(struct online_user** list, const char* account, const char* username) {
    struct online_user* new_user = malloc(sizeof(struct online_user));
    if (!new_user) {
        perror("内存分配失败");
        return;
    }

    strncpy(new_user->account, account, ACCOUNT_LENGTH);
    strncpy(new_user->username, username, 29);
    new_user->next = *list;
    *list = new_user;
}

void free_online_users(struct online_user* list) {
    while (list != NULL) {
        struct online_user* temp = list;
        list = list->next;
        free(temp);
    }
}